import sys
from PyQt6 import QtWidgets, QtCore


# 如果使用 PyQt6，导入方式略有不同：
# from PyQt6 import QtWidgets, QtCore

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        # 设置窗口标题和大小
        self.setWindowTitle("PyQt 示例")
        self.setGeometry(100, 100, 400, 300)

        # 创建中央部件
        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)

        # 创建布局
        layout = QtWidgets.QVBoxLayout()
        central_widget.setLayout(layout)

        # 创建标签
        self.label = QtWidgets.QLabel("点击按钮改变文本")
        layout.addWidget(self.label)

        # 创建按钮
        self.button = QtWidgets.QPushButton("点击我")
        self.button.clicked.connect(self.on_button_click)
        layout.addWidget(self.button)

        # 创建文本输入框
        self.text_input = QtWidgets.QLineEdit()
        self.text_input.setPlaceholderText("输入文本并按下回车")
        self.text_input.returnPressed.connect(self.on_text_entered)
        layout.addWidget(self.text_input)

    def on_button_click(self):
        self.label.setText("按钮被点击了!")

    def on_text_entered(self):
        text = self.text_input.text()
        self.label.setText(f"你输入了: {text}")
        self.text_input.clear()


def main():
    # 创建应用程序实例
    app = QtWidgets.QApplication(sys.argv)

    # 创建并显示主窗口
    window = MainWindow()
    window.show()

    # 运行应用程序主循环
    sys.exit(app.exec())


if __name__ == "__main__":
    main()
